<template>
	<view>
		<view>
			<table-title content="合同列表"></table-title>
			<view class="choice-bar">
				<view class="nav-choice" :class="isActive[index]" v-for="(value,index) in navChoice" :key="index"
					@click="changeActive(index)">{{value}}</view>
			</view>
			<aircondition-record v-for="(item,index) in recordList" :key="index"
				:company="item.company" :building="item.building" :time="item.time"
				:state="item.state" @getClick="toContractDetails(item.contractFileId,item.configFileId)"></aircondition-record>
		</view>
	</view>
</template>

<script>
	import {
		ONLINE_SERVER_ADDRESS_BASE,
		syncGetOrder,
	} from "../../../util/network.js"
	export default{
		data() {
			return {
				userInfo:'',
				chooseState: '',
				navChoice: ['当前合同','失效合同'],
				isActive: ['active-choice-contract','nav-choice-contract'],
				activeIndex: 0,
				recordList:[],
				current: 0
			}
		},
		methods: {
			changeActive(e) {
				if(e!==this.activeIndex){
					let tmp = [...this.isActive]
					tmp[e] = 'active-choice-contract'
					tmp[this.activeIndex] = 'nav-choice-contract'
					this.activeIndex = e
					this.isActive = tmp
					this.choosedState = this.navChoice[this.activeIndex]
					if(this.activeIndex == 0){
						this.getCurrent()
					}else{
						this.getHistoryList()
					}
				}
				for(let i=0; i<this.recordList.length; i++){
					this.recordList[i].state = this.choosedState
				}
			},
			toContractDetails(contractFileId,configFileId) {
				let temp = JSON.stringify(contractFileId)
				uni.navigateTo({
					url:'../details/details?contractFileId=' + temp + '&configFileId=' + configFileId
				})
			},
			getCurrent(){
				this.recordList = []
				let temp = {'company':'','building':'',
						'time':'','contractFileId':[],'configFileId':-1,'state':'当前合同'}
				temp.company = this.userInfo.companyName
				try{
					temp.time ='合同有效期:' + this.userInfo.dueTime.replace("T", ' ')
				}catch(e){
					//TODO handle the exception
				}
				try{
					temp.contractFileId = this.userInfo.contractFileId.split(',')
				}catch(e){
					//TODO handle the exception
				}
				temp.configFileId = this.userInfo.configFileId
				if(typeof(temp.contractFileId) == "undefined" || typeof(temp.configFileId) == "undefined"){
					uni.showModal({
						content:'未找到当前合同，如果第一次入驻，请完成收楼业务',
						showCancel:false
					})
					return 0
				}else{
					this.recordList.push(temp)
				}
			},
			getHistoryList(){
				this.recordList = []
				let token = uni.getStorageSync('token')
				let _this = this
				uni.request({
					url:`${ONLINE_SERVER_ADDRESS_BASE}/user/my-history-contract`,
					header: {
						'Authorization': token,
					},
					method:'POST',
					success: function (res) {
						console.log("历史合同",res)
						if(res.data.code == 200){
							try{
								res.data.data.contractFileList.forEach(item => {
									let temp = {'company':'','building':'',
											'time':'','contractFileId':[],'configFileId':-1,'state':'历史合同'}
									temp.company = _this.userInfo.companyName
									try{
										temp.time =''
									}catch(e){
										//TODO handle the exception
									}
									try{
										temp.contractFileId = item.contractFileId.split(',')
									}catch(e){
										//TODO handle the exception
									}
									temp.configFileId = item.configFileId
									_this.recordList.push(temp)
								})
								
							}catch(e){
								//TODO handle the exception
							}
						}
					}
				})
			}
		},
		onShow() {
			let _this = this
			let userId = uni.getStorageSync('userinfo').id
			let token = uni.getStorageSync('token')
			if(this.activeIndex == 0){
				uni.request({
					url:`${ONLINE_SERVER_ADDRESS_BASE}/user/show/` + userId,
					header: {
						'Authorization': token,
					},
					method:'GET',
					success: function (res) {
						console.log("用户信息",res.data);
						if(res.data.code == 200){
							if(res.data.data.user.invoiceSetting != null){
								_this.userInfo = res.data.data.user
							}else{
								_this.userInfo = uni.getStorageSync('userinfo')
							}
							_this.getCurrent()
						}
					}
				})
			}else{
				this.getHistoryList()
			}
		}
	}
</script>

<style>
	page{
		background-color: #F1F1F1
	}
	.nav-choice-contract{
		height: 100rpx;
		width: 387rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 30rpx;
		border-bottom: solid 5rpx #F1F1F1;
	}
	.choice-bar{
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content:center;
		width: 750rpx;
		color: #808080;
	}
	.active-choice-contract{
		height: 100rpx;
		width: 387rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 30rpx;
		font-weight:600;
		color: #DD524D;
		border-bottom: solid 5rpx #DD524D;
	}
</style>
